
Ext.define('Admin.view.department.DepartmentViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.departmentViewController',

    onShowPreviewButtonClick: function(btn) {
      		alert("Hello");
    }
    ,openAddWindow: function(view, recIndex, cellIndex, item, e, record) {
    	var cfg = Ext.apply({
            xtype: 'departmentEditWindow',
			items: [Ext.apply({xtype: 'departmentAddForm'})]
		});
		var win = Ext.create(cfg);	
    }
     ,openEditWindow: function(view, recIndex, cellIndex, item, e, record) {
    	var cfg = Ext.apply({
            xtype: 'departmentEditWindow',
			items: [Ext.apply({xtype: 'departmentEditForm'})]
		});
		var win = Ext.create(cfg);
		
		win.down("form").getForm().loadRecord(record);//加载修改数据到Form
	
    }

    ,submitEditForm: function(btn) {
		var form = btn.up('form');
			form.getForm().submit({       
				url:'department/saveOrUpdate',
				//waitMsg: '正在上传，请耐心等待....',
				success: function(form, action){    
					Ext.Msg.alert('提示', action.result.msg,function(){
						btn.up('window').close();
						//form.getViewModel().getStore('departmentDataList').reload();
	                    Ext.getCmp('departmentGridPanel').getStore().reload();
						
					});       
				},       
				failure: function(form, action){
					Ext.Msg.alert('提示', action.result.msg);
				}
			});
	}

    ,deleteDepartment: function(view, recIndex, cellIndex, item, e, record) {
      		Ext.MessageBox.confirm('提示', '确定要进行删除操作吗？数据将无法还原！',
      			function(btn, text){
                	if(btn=='yes'){
                			Ext.Ajax.request({ 
								url : 'department/delete', 
								method : 'post', 
								params : { 
									id :record.get('id')
								}, 
								success: function(response, options) {
					                var json = Ext.util.JSON.decode(response.responseText);
						            if(json.success){
						            	Ext.Msg.alert('系统提示', json.msg, function() {
						                    view.getStore().reload();
						                });
							        }else{
							        	 Ext.Msg.alert('系统提示', json.msg);
							        }
					            }
							});
               		}
            	}
            , this);
    }
    
     ,deleteDepartments: function(btn) {
		var grid = btn.up('gridpanel');
		var selModel = grid.getSelectionModel();
		if(selModel.hasSelection()){
			Ext.MessageBox.confirm('警告', '确定要进行批量删除操作？数据将无法还原！',
      		function(button){
                	if(button=='yes'){
                			var selected = selModel.getSelection();
		                    var selectIds = []; //要删除的id
		                    Ext.each(selected, function (item) {
		                        selectIds.push(item.data.id);
		                    })

                			Ext.Ajax.request({ 
								url : 'department/deleteDepartments', 
								method : 'post', 
								params : { 
									ids:selectIds
								}, 
								success: function(response, options) {
					                var json = Ext.util.JSON.decode(response.responseText);
						            if(json.success){
						            	Ext.Msg.alert('系统提示', json.msg, function() {
						                   grid.getStore().reload();
						                });
							        }else{
							        	 Ext.Msg.alert('系统提示', json.msg);
							        }
					            }
							});
               			}
            		});
		}
		 else {
            Ext.Msg.alert("错误", "没有任何行被选中，无法进行删除操作！");
        }
   }
   
   ,search: function(view, recIndex, cellIndex, item, e, record) {
		var searchField = this.lookupReference('searchFieldName').getValue();
		var searchValue = this.lookupReference('searchFieldValue').getValue();
		
		var store = Ext.getCmp('departmentGridPanel').getStore();
		
		Ext.apply(store.proxy.extraParams, {name:"",nature:""});
			
		if(searchField==='name'){
			Ext.apply(store.proxy.extraParams, {name:searchValue});
		}
		if(searchField==='nature'){
			Ext.apply(store.proxy.extraParams, {nature:searchValue});
		}
		
		store.load({params:{start:0, limit:17, page:1}});
		
    }


});

      	